<template>
	<view class="pay-view">
		<icon-title comTitle="微信扫码支付" desc></icon-title>
		<view class="wx-codeImage-view">
			<image class="wx-codeImage" :src="wechat" mode="widthFix"></image>
		</view>
		<icon-title comTitle="上传支付凭证" desc></icon-title>
		<view class="wx-pay-uploadView">
			<view class="market-upload-imageview">
				<image class="marketImage" v-if="form.voucher" @click='uploadImage' :src="form.voucher"></image>
				<view class="market-upload-image" @click="uploadImage" v-if="!form.voucher">
					<text style="font-size: 26px;">+</text>
				</view>
			</view>
		</view>
		<view class="pay-btn-view">
			<bottom-bottom name="确认" @click="credentials"></bottom-bottom>
		</view>
	</view>
</template>

<script>
	import iconTitle from "@/components/title/icon-title.vue"
	import bottomBottom from "@/components/button/bottomButton.vue"
	export default {
		components: {
			iconTitle,
			bottomBottom
		},
		data() {
			return {
				wechat: '',
				form: {
					order_sn: '',
					voucher: "",
				},
				form1: {},
				// 防抖
				isSubmit:true,
				timer:''
			}
		},
		onLoad(option) {
			if (option.form) {
				this.form1 = JSON.parse(option.form)
				console.log(this.form1)
			}
		},
		onShow() {
			this.getpaytype()
		},
		methods: {
			// 获取支付方式
			getpaytype() {
				this.$http.post(this.$api.getpaytype, {}, {
					token: true
				}).then(res => {
					this.wechat = res.data.wechat
				})
			},
			uploadImage() {
				let that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'],
					success: function(res) {
						uni.uploadFile({
							url: that.$api.upload + 'Upload/image',
							filePath: res.tempFilePaths[0],
							name: 'file',
							header: {
								'token': uni.getStorageSync('token'),
							},
							success: res => {
								let result = JSON.parse(res.data);
								that.form.voucher = result.data.uri;
							}
						});
					}
				});
			},
			// 提交凭证
			credentials() {
				if (!this.form.voucher) {
					return this.$tools.toast('请上传支付凭证')
				}
				if(!this.isSubmit) return false
				this.isSubmit = false
				// 创建订单
				this.$http.post(this.$api.createOrder, this.form1, {
					token: true
				}).then(res => {
					if (res.code == 1) {
						this.form.order_sn = res.data.order_sn
						this.credentialsData()
					}
				})
			},
			// 凭证方法
			credentialsData() {
				clearTimeout(this.timer)
				this.$http.post(this.$api.credentials, this.form, {
					token: true
				}).then(res => {
					if (res.code == 1) {
						this.$tools.toast('提交成功')
						this.$tools.switchTab('/pages/index/index',500)
						this.timer = setTimeout(function(){
							this.isSubmit = true
						},500)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	// .bg-box-view{
	// 	background-color: #FFF0CA;
	// }
	.wx-pay-uploadView {
		margin-top: 30rpx;
		background-color: #FFF0CA;
		padding: 60rpx 30rpx;
		border-radius: 30rpx;
	}

	.market-upload-imageview {
		display: flex;
		align-items: center;
		justify-content: center;

		.market-upload-image {
			width: 236rpx;
			height: 236rpx;
			border-radius: 30rpx;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.marketImage {
			width: 236rpx;
			height: 236rpx;
			border-radius: 30rpx;
			margin-right: 10rpx;
		}
	}

	.wx-codeImage-view {
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FFF0CA;
		padding: 60rpx 30rpx;
		border-radius: 30rpx;
		.wx-codeImage {
			width: 360rpx;
			height: 360rpx;
		}
	}

	.pay-btn-view {
		// position: absolute;
		// bottom: 80rpx;
		// width: 92%;
		margin: 200rpx 0;
	}

	.pay-view {
		padding: 0 30rpx;
		overflow: hidden;
		// background-color: #FFE6E6;
		min-height: 100vh;
	}
</style>